from flask import Flask, request, render_template
import time

from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/upload', methods=['POST'])
def upload():
    # f = request.files['file1']
    # f.save('./file1.png')

    t1 = time.time()

    f = request.files

    # filetype = f.mimetype.split('/')[1]
    # strfNow = time.strftime('%Y%m%d-%H%M%S', time.localtime())
    strfNow = time.strftime('%Y%m%d', time.localtime())

    for key, item in f.items():
        print(key)
        saveFile = strfNow + '_' + item.filename.split('.')[0] + '_.' + item.filename.split('.')[1]
        item.save('upload/' + saveFile)

    # saveFile = f.filename.split('.')[0] + '_' +str(int(time.time())) + '_.' + f.filename.split('.')[1]
    # saveFile = f.filename.split('.')[0] + '_' + strfNow + '_.' + f.filename.split('.')[1]
    #
    # f.save(saveFile)

    t2 = time.time()

    return '上传成功, 耗时(%s秒)！' % str(int(t2-t1))

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

    # http_server = WSGIServer(('0.0.0.0', 5000), app)
    # http_server.serve_forever()